package fr.vahren.dotastrat.game;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.Socket;
import java.util.Map;

import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;

import fr.vahren.dotastrat.utils.D2SJSONUtils;

public class D2SGameServerThread extends Thread {

	private Map<Integer, D2SGame> games;
	private Socket client;

	public D2SGameServerThread(Socket client, Map<Integer, D2SGame> games) {
		this.games = games;
		this.client = client;
	}

	@Override
	public void run() {

		BufferedReader in = null;
		BufferedWriter out = null;
		try {
			in = new BufferedReader(new InputStreamReader(
					client.getInputStream()));
			out = new BufferedWriter(new OutputStreamWriter(
					client.getOutputStream()));

			StringBuffer msg = new StringBuffer();

			String line = null;
			while ((line = in.readLine()) != null) {
				if (!"#END#".equals(line)) {
					msg.append(line);
				} else {
					// stop reading
					break;
				}
			}

			// DO SOMETHING!
			String response = processMessage(msg);

			// send response
			out.write(response);
			out.newLine();
			out.write("#END#");
			out.newLine();
			out.flush();

		} catch (IOException e) {
			System.err.println("An error occured");
			e.printStackTrace();
		} finally {
			try {
				if (in != null) {
					in.close();
				}
				if (out != null) {
					out.close();
				}
			} catch (IOException e) {
				System.err.println("An error occured");
				e.printStackTrace();
			}
		}

	}

	private String processMessage(StringBuffer msg) {
		// parse the JSON!
		JSONParser parser = new JSONParser();

		try {
			JSONObject request = (JSONObject) parser.parse(msg.toString());

			return processJSONMessage(request).toJSONString();

		} catch (ParseException e) {
			e.printStackTrace();
			JSONObject errorResponse = new JSONObject();
			errorResponse.put("error", e.getMessage());
			return errorResponse.toJSONString();
		}

	}

	private JSONObject processJSONMessage(JSONObject request) {
		// get the action and game id
		String action = (String) request.get("action");
		int gameId = Integer.parseInt((String) request.get("gameId"));
		D2SGame game = games.get(gameId);

		if ("info".equals(action)) {
			return game.info(request);
		} else if ("act".equals(action)) {
			return game.act(request);
		} else if ("buy".equals(action)) {
			return game.buy(request);
		} else if ("level".equals(action)) {
			return game.level(request);
		} else if ("buyback".equals(action)) {
			return game.buyBack(request);
		} else if ("end".equals(action)) {
			return game.end(request);
		} else if ("start".equals(action)) {
			return game.start(request);
		} else {
			return D2SJSONUtils.errorJson("Wrong action type = " + action);
		}

	}

}
